﻿using System.Collections.Generic;
using CTEM.Models;
using CTEM.Controllers.DAO;
using System;
using CTEM.Controllers.Global;

namespace CTEM.Controllers.BUS
{
    public class VehicleBUS
    {
        private readonly VH_VehicleDAO _vehicleDAO = new VH_VehicleDAO();

        #region Business Layer for Vehicle Entity
        /// <summary>
        /// 
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="errorMessage"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool ValidateVehicle(VH_VehicleDTO vehicle, out string errorMessage, out string fieldName)
        {
            vehicle.Name = Global.Global.TitleCase(vehicle.Name);
            vehicle.Name = Global.Global.RemoveRedundantSpace(vehicle.Name);
            if (vehicle.Name.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Tên Cung Cấp";
                fieldName = "VehicleName";
                return false;
            }
            else if (vehicle.Name.Length > 50)
            {
                errorMessage = "Tên Nhà Cung Cấp Không Được Dài Quá 50 Ký ";
                fieldName = "VehicleName";
                return false;
            }
            else if (vehicle.ManagerID == 0)
            {
                errorMessage = "Bạn Chưa Chọn Người Quản Lý";
                fieldName = "ManagerID";
                return false;
            }
            
            else if (vehicle.Name.Equals(Constants.EMPTY_TEXT))
            {
                List<VH_VehicleDTO> listsubcon = LoadVehicle(false);
                foreach (var item in listsubcon)
                {
                    if (item.Name.Equals(vehicle.Name) && item.VehicleID != vehicle.VehicleID)
                    {
                        errorMessage = "Tên Nhà Cung Cấp Bạn Vừa Nhập Đã Tồn Tại";
                        fieldName = "VehicleName";
                        return false;
                    }
                }
            }
            errorMessage = "";
            fieldName = "";
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vehicleDTO"></param>
        /// <returns></returns>
        public long CreateVehicle(VH_VehicleDTO vehicleDTO)
        {
            vehicleDTO.Name = Global.Global.TitleCase(vehicleDTO.Name);
            vehicleDTO.Name = Global.Global.RemoveRedundantSpace(vehicleDTO.Name);
            return _vehicleDAO.CreateVehicle(vehicleDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vehicleID"></param>
        /// <returns></returns>
        public bool DeleteVehicle(long vehicleID)
        {
            return _vehicleDAO.DeleteVehicle(vehicleID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isactive"></param>
        /// <returns></returns>
        internal List<VH_VehicleDTO> LoadVehicle(bool isactive)
        {
            return _vehicleDAO.LoadAllVehicles(isactive);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vehicleDTO"></param>
        /// <returns></returns>
        internal bool UpdateVehicle(VH_VehicleDTO vehicleDTO)
        {
            vehicleDTO.UpdatedBy = Global.Global.CurrentUser.Username;
            vehicleDTO.LastUpdated = DateTime.Today;
            vehicleDTO.Name = Global.Global.TitleCase(vehicleDTO.Name);
            vehicleDTO.Name = Global.Global.RemoveRedundantSpace(vehicleDTO.Name);
            return _vehicleDAO.UpdateVehicle(vehicleDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vehicleID"></param>
        /// <returns></returns>
        internal VH_VehicleDTO GetVehicleByID(long vehicleID)
        {
            return _vehicleDAO.GetVehicleByID(vehicleID);
        }
        #endregion
    }
}
